package com.sgxy.wmservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sgxy.commonutils.R;
import com.sgxy.wmservice.entity.WmGoods;
import com.sgxy.wmservice.entity.WmSupplier;
import com.sgxy.wmservice.service.WmSupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-04-29
 */
@RestController
@RequestMapping("/wmservice/wmsupplier")
public class WmSupplierController {
    @Autowired
    private WmSupplierService wmSupplierService;
    //获取分页供货商列表
    @PostMapping("{page}/{limit}")
    public R index(@PathVariable Long page,
                   @PathVariable Long limit,
                   @RequestBody(required = false)WmSupplier wmSupplier){
        Page<WmSupplier> supplierPage = new Page<>(page,limit);
        QueryWrapper<WmSupplier> supplierQueryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(wmSupplier.getSupplierName())) {
            supplierQueryWrapper.like("supplier_name", wmSupplier.getSupplierName());
        }
        wmSupplierService.page(supplierPage, supplierQueryWrapper);
        return R.ok().data("items", supplierPage.getRecords()).data("total", supplierPage.getTotal());
    }
    @GetMapping("getSupplierAll")
    public R getSupplierAll(){
        List<WmSupplier> list = wmSupplierService.list(null);
        return R.ok().data("list",list);
    }
    //根据id获得供货商
    @GetMapping("getSupplier/{id}")
    public R getSupplier(@PathVariable String id){
        WmSupplier wmSupplier = wmSupplierService.getById(id);
        return R.ok().data("supplier", wmSupplier);
    }


    //更新供货商信息
    @PostMapping("updateSupplier")
    public R updateSupplier(@RequestBody WmSupplier wmSupplier){
        wmSupplierService.updateById(wmSupplier);
        return R.ok();
    }

    //添加供货商信息
    @PostMapping("addSupplier")
    public R addSupplier(@RequestBody WmSupplier wmSupplier){
        wmSupplierService.save(wmSupplier);
        return R.ok();
    }

    //删除角色
    @DeleteMapping("{id}")
    public R deleteSupplierById(@PathVariable String id){
        boolean flag = wmSupplierService.deleteWmSupplierById(id);
        return flag?R.ok():R.error();
    }

}
